<?php

class GoodsTypeModel extends Model {
    protected $_logic_table = 'goods_type';

    public function getGoodsTypes() {
        $sql = "SELECT * FROM {$this->_table} ORDER BY type_id";
        return $this->_dao->getAll($sql);
    }

    public function getGoodsTypesPage($offset, $pagesize) {
        $sql = "select *,(select count(*) from cz_attribute where cz_attribute.type_id = {$this->_table}.type_id) as attr_count from {$this->_table} ORDER BY type_id LIMIT $offset, $pagesize";
        return $this->_dao->getAll($sql);
    }

    //select a.*, count(b.type_id) as attr_count from cz_goods_type as a inner join cz_attribute as b on a.type_id = b.type_id where a.type_id = 5;
    //SELECT count(type_id) from cz_attribute where type_id = 5;
    //select *,(select count(*) from cz_attribute where cz_attribute.type_id = cz_goods_type.type_id) as attr_count from cz_goods_type;
    public function getGoodsTypeAttrCount($type_id) {
        $sql = "SELECT count(type_id) FROM cz_attribute WHERE type_id = $type_id";
        return $this->_dao->getOne($sql);
    }
}